package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dbOperator.AdvancedOperation;
import dbOperator.BasicOperation;

/**
 * Servlet implementation class Borrow_Return
 */
@WebServlet("/Borrow_Return")
public class Borrow_Return extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int type = Integer.parseInt(request.getParameter("Type3"));
		try {
			if (type == 1)
				returnBook(request, response);
			else if (type == 2)lendBook(request, response);
			response.sendRedirect(request.getHeader("Referer"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void returnBook(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException, ParseException {
		Cookie cookies[] = request.getCookies();
		if (cookies == null) {
			response.sendRedirect("index.html");
			return;
		}
		for (Cookie c : cookies) {
			if (c.getName().equals("LoginInfo")) {
				String currentCookie = c.getValue();
				Connection con = BasicOperation.getConnection();
				int uid = AdvancedOperation.getUidbyCookie(con, currentCookie);
				String bookID = request.getParameter("rBookID");
				String readerIDstr = request.getParameter("readerID4");
				String finestr = request.getParameter("fine");
				if (bookID == null)
					return;
				int readerID=0;
				if(readerIDstr==null||readerIDstr.equals(""))return;
				readerID=Integer.parseInt(readerIDstr);
				double fine;
				if(finestr==null||finestr.equals(""))fine=0;
				else fine=Double.parseDouble(finestr);
				dbOperator.AdvancedOperation.getBookReturned(con, bookID, uid,readerID,fine);
				con.close();
				return;
			}
		}
	}

	public void lendBook(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException, ParseException {
		Cookie cookies[] = request.getCookies();
		if (cookies == null) {
			response.sendRedirect("index.html");
			return;
		}
		for (Cookie c : cookies) {
			if (c.getName().equals("LoginInfo")) {
				String currentCookie = c.getValue();
				Connection con = BasicOperation.getConnection();
				int uid = AdvancedOperation.getUidbyCookie(con, currentCookie);
				String ReaderIDstr = request.getParameter("ReaderID");
				if (ReaderIDstr == null||ReaderIDstr.equals(""))
					return;
				int ReaderID=Integer.parseInt(ReaderIDstr);
				String bookID = request.getParameter("BookID");
				if (bookID == null)
					return;
				bookID=(bookID.replace(" ","")).trim();
				dbOperator.AdvancedOperation.getBookBorrowed(con, bookID, uid, ReaderID);
				con.close();
				return;
			}
		}
	}

}
